/**
 * This example turns the ESP32 into a Bluetooth LE mouse that scrolls down every 2 seconds.
 */
#include <BleMouse.h>
#define Touch1 15
#define Touch2 2
#define Touch3 0
#define Touch4 4

BleMouse bleMouse("YuBTMouse","YuCC",100);

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");
  bleMouse.begin();
}

#define SERIAL_BUFFER_SIZE 64
char serialBuffer[SERIAL_BUFFER_SIZE];
int serialBufferIndex = 0;

#define TouchSens 20

void loop() {
  if(bleMouse.isConnected()) {
    //Serial.println("Scroll Down");
    //bleMouse.move(0,0,-1);
    if(Serial.available()>0)
    {
      char inChar=Serial.read();
      if (inChar == 3) {
        serialBuffer[serialBufferIndex] = 0; // 添加字符串结束符
        handleCommand(serialBuffer); // 处理接收到的命令
        serialBufferIndex = 0; // 重置缓冲区索引
      } else if (serialBufferIndex < SERIAL_BUFFER_SIZE - 1) {
      // 将字符添加到缓冲区
        serialBuffer[serialBufferIndex++] = inChar;
      }
    }
    int vt1 = touchRead(Touch1);
    if(vt1<TouchSens)
    {
       Serial.printf("Touch1 pressed = %d \n\r",vt1);
       fastForward();
       delay(200);
    }
    int vt4 = touchRead(Touch4);
    if(vt4<TouchSens)
    {
      Serial.printf("Touch4 pressed = %d \n\r",vt4);
      fastBackward();
      delay(200);
    }
  }
  else
  {
      //Serial.println("Waiting to connect..");
      delay(1000);
  }
}

void sendKeydown()
{
  char* s="p1";
  handleCommand(s);
  delay(50);
}

void sendKeyup()
{
  char* s="r1";
  handleCommand(s);
  delay(50);
}

void sendMove(int dx,int dy)
{
  byte s[4]={'m','P','P',0};
  s[1]=80+dx;
  s[2]=80+dy;
  handleCommand((char *)s);
  delay(20);
}
void resetPos()
{
  for(int i=0;i<30;i++)  sendMove(-30,-20);
  for(int i=0;i<10;i++)  sendMove(30,13);
}

void fastForward()
{
  Serial.println("sendKeydown");
  sendKeydown();
  Serial.println("sendmove 20,0");
  sendMove(20,0);
  Serial.println("sendmove 20,0");
  sendMove(20,0);
  Serial.println("sendmove 20,0");
  sendMove(20,0);
  Serial.println("send keyup");
  sendKeyup();
  Serial.println("reset pos");
  resetPos();
}
void fastBackward()
{
  sendKeydown();
  sendMove(-20,0);
  sendMove(-20,0);
  sendMove(-20,0);
  sendKeyup();
  resetPos();
}

const int speed=2;

void handleCommand( const char *command) {
  Serial.println(command);

  // if (strcmp(command,"ml")== 0) {
  //   bleMouse.move(-speed,0,0,0);
  //   //digitalWrite(LED_BUILTIN, HIGH); // 打开内置LED
  // } else if (strcmp(command,"mr")== 0) {
  //   bleMouse.move(speed,0,0,0);
  // } else if (strcmp(command,"mu")== 0) {
  //   bleMouse.move(0,-speed,0,0);
  // }
  // else if (strcmp(command,"md")== 0) {
  //   bleMouse.move(0,speed,0,0);
  // }
  // else 
  if(command[0]=='m'){
    int dx = command[1]-80;
    int dy = command[2]-80;
    Serial.printf("%s : %d , %d \n\r",command,dx,dy);
    bleMouse.move(dx * speed,dy * speed,0,0);
  }
  else if (command[0]=='p') {
    if(command[1]=='1')
      bleMouse.press(MOUSE_LEFT);
    else if(command[1]=='2')
      bleMouse.press(MOUSE_RIGHT);
    else
      bleMouse.press(MOUSE_MIDDLE);
  }
  else if (command[0]=='r') {
    if(command[1]=='1')
      bleMouse.release(MOUSE_LEFT);
    else if(command[1]=='2')
      bleMouse.release(MOUSE_RIGHT);
    else
      bleMouse.release(MOUSE_MIDDLE);
  }
  else if (strcmp(command,"1")== 0) {
    bleMouse.click();
  }
  else {
    Serial.println("Unknown command"); // 未知命令
  }
}
